package com.nuanshui.heatedloan.extension.jpasearch;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

/**
 * 逻辑条件表达式 用于复杂条件时使用，如但属性多对应值的OR查询等
 * 
 * @author lee
 * 
 */
public class LogicalExpression implements Criterion {
	private final Criterion[] criterion; // 逻辑表达式中包含的表达式
	private final Operator operator; // 计算符

	public LogicalExpression(final Criterion[] criterions,
			final Operator operator) {
		this.criterion = criterions;
		this.operator = operator;
	}

	public LogicalExpression(final List<Criterion> criterions) {
		criterion = new Criterion[criterions.size()];
		for (int i = 0; i < criterions.size(); i++) {
			criterion[i] = criterions.get(i);
		}

		this.operator = Operator.OR;
	}

	public LogicalExpression(final Criterion[] criterions) {
		this.criterion = criterions;
		this.operator = Operator.OR;
	}

	@Override
	public Predicate toPredicate(final Root<?> root,
			final CriteriaQuery<?> query, final CriteriaBuilder builder) {
		List<Predicate> predicates = new ArrayList<Predicate>();
		for (int i = 0; i < this.criterion.length; i++) {
			predicates.add(this.criterion[i].toPredicate(root, query, builder));
		}
		switch (operator) {
		case OR:
			return builder.or(predicates.toArray(new Predicate[predicates
					.size()]));
		default:
			return null;
		}
	}

}